package myServlet;


import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;

import org.apache.commons.beanutils.BeanUtils;

import util.UUIDUtils;
import bean.MenuBean;
import bean.RoleBean;
import databaseDAO.Menu2Dao;
import databaseDAO.RoleManagement;



@WebServlet("/admin/roleServlet")
public class RoleServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	
	
	public void doPost(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException{
		doGet(request,response);
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException,IOException{
		String state = request.getParameter("m");  
		
		try{
				if(state.equals("findAll")){
					// 如果是login 则允许登录		
					
						this.findAll(request, response);
					
				}else if(state.equals("update")){
					this.update(request, response);
					
				}else if(state.equals("addRole")){
					this.addRole(request,response);
				}else if(state.equals("del")){
					
					this.delRole(request,response);
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	private void delRole(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		// TODO 自动生成的方法存根
		//System.out.println("del角色");
		RoleManagement rm = new RoleManagement();
		String roleID = request.getParameter("roleID");
		System.out.println("进来的角色id: "+roleID);
		rm.delRoleByID(roleID);
		
		this.findAll(request, response);
	}

	private void addRole(HttpServletRequest request,
			HttpServletResponse response) {
		int ok=0;
		RoleBean rb = new RoleBean();
		String roleID = UUIDUtils.getId();
		String nickName = request.getParameter("roleNickName");
		String roleName = request.getParameter("roleName");
		String roleDescription = request.getParameter("roleDescription");
		
		rb.setRoleID(roleID);
		rb.setNickName(nickName);
		rb.setRoleName(roleName);
		rb.setRoleDescription(roleDescription);
		
		RoleManagement rm = new RoleManagement();
		try {
			ok = rm.insertNewRole(rb);
			if(ok==1){
				response.setContentType("text/html;charset=utf-8");
				response.getWriter().println("添加角色成功");
			}
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		
		
	}

	public void destroy(){
	}
	
	public void doError(HttpServletRequest request, HttpServletResponse response,String error)
			throws ServletException,IOException{ // 错误处理
		request.setAttribute("error", error);  // 设置属性
		// 然后转发
//		System.out.println("ERROR");
		request.getRequestDispatcher("/error.jsp").forward(request, response);  // 未定义
		// 转发至products页面
	}
	
	
	
	/**
	 * 获取所有角色
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public void findAll(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		
		
		System.out.println("---------------finadAll----------------------");
		// 0. 获取所有角色
		RoleManagement rm = new RoleManagement();
		List<RoleBean> roles = rm.queryRoles();
		

		// 1.获取菜单项
		Menu2Dao m2d = new Menu2Dao();
		List<MenuBean> menus = m2d.queryMenu();  // 菜单 
		// 2.
		HttpSession session = request.getSession(false);
		session.setAttribute("roles", roles);
		session.setAttribute("menusList", menus);
		
		response.sendRedirect("admin/managerRole.jsp");	
		
	}
	
	/**
	 * 更新角色权限
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public String update(HttpServletRequest request, HttpServletResponse response) throws Exception
	{
		// 0. 获取传递的参数
		String menuid = request.getParameter("menuID");
		String stat = request.getParameter("stat");
		String roleid = request.getParameter("roleID");
		Menu2Dao m2d = new Menu2Dao();
		if(stat.equalsIgnoreCase("true")) // 增加权限
		{
			//System.out.println("顺达te");
			
			m2d.insertToRoleright(roleid,menuid);
		    this.findAll(request, response);
			//roleDao.insertToRoleright(roleid, menuid);
		}
		else	// 删除权限
		{
			m2d.deleteFromRolerigt(roleid, menuid);
			this.findAll(request, response);
		}

		return null;
	}
	
}
